﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using EntityObjects;
//using iTextSharp.text;
//using iTextSharp.text.pdf;
using System.IO;

namespace PresentationLayer.View.DepartmentRepresentative
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        public ViewCatalogueForClerkContol control;
        protected void Page_Load(object sender, EventArgs e)
        {
            control = new ViewCatalogueForClerkContol();
            if (!IsPostBack)
            {
                ddCategory.DataSource = control.getCategory();
                ddCategory.DataValueField = "CategoryId";
                ddCategory.DataTextField = "CategoryName";
                ddCategory.DataBind();
                List<String> str = control.getBin();
                foreach (string st in str)
                {
                    ddCategory0.Items.Add(st);
                }
                ddCategory0.DataBind();
                foo.DataSource = control.getCatalogue();
                foo.DataBind();
            }
        }

        protected void ddCategory0_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            //foo.DataSource = control.searchByCategory(Convert.ToInt16(ddCategory.SelectedValue));
            //foo.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt16(ddCategory.SelectedValue);
            String bin = ddCategory0.SelectedItem.ToString();
            String desc = TextBox1.Text.ToString();
            if (bin == "All")
                foo.DataSource = control.searchWithoutBin(id, desc);
            else if (bin != "All") foo.DataSource = control.searchWithBin(id, desc, bin);
            foo.DataBind();
        }

        protected void foo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //int i = foo.PageIndex;
            //if (ViewState[i.ToString()] == null) {
            //    ViewState[i.ToString()] = foo.DataSource;
            //    foo.PageIndex = e.NewPageIndex;
            //}            
            //foo.DataBind();
        }

        protected void foo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            TextBox tb = (TextBox)foo.SelectedRow.Cells[7].FindControl("txtQty");
            string txt = tb.Text;
            Label1.Text = tb.Text;
        }

        protected void lblTable_Click(object sender, EventArgs e)
        {
            //var doc = new Document();

            //PdfPTable t = new PdfPTable(8);

            //PdfPCell c = new PdfPCell(new Phrase("Catalogue List"));
            //c.HorizontalAlignment = 1;
            //c.Colspan = 8;

            //string pdfPath = Server.MapPath("PDFs");
            //PdfWriter.GetInstance(doc, new FileStream(pdfPath + "/catalogue.pdf", FileMode.Create));
            //doc.PageCount = foo.PageCount;
            //doc.Open();

            //t.AddCell(c);
            //t.AddCell("Serial No");
            //t.AddCell("Item Code");
            //t.AddCell("Category");
            //t.AddCell("Name");
            //t.AddCell("Reorder Leverl");
            //t.AddCell("Reorder Qty");
            //t.AddCell("Unit");
            //t.AddCell("Bin");
            //foreach (GridViewRow gvr in foo.Rows)
            //{
            //    for (int j = 0; j <= gvr.Cells.Count - 1; j++)
            //    {
            //        t.AddCell(gvr.Cells[j].Text.Trim().ToString());
            //    }
            //}
            //doc.Add(t);
            //doc.Close();
        }

        //PdfReader r = new PdfReader("/catalogue.pdf");

        protected void foo_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}